package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.vo.OptionsVo;
import com.tanhua.dubbo.server.vo.SoulQuestionVo;
import com.tanhua.mapper.OptionMapper;
import com.tanhua.mapper.PaperMapper;
import com.tanhua.mapper.ReportMapper;
import com.tanhua.mapper.SoulQuestionMapper;
import com.tanhua.pojo.Option;
import com.tanhua.pojo.Paper;
import com.tanhua.pojo.Report;
import com.tanhua.pojo.SoulQuestion;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;


@Service(version = "1.0.0")
public class TestSoulApiImp implements TestSoulApi {

    @Autowired
    SoulQuestionMapper soulQuestionMapper;
    @Autowired
    ReportMapper reportMapper;
    @Autowired
    OptionMapper optionMapper;
    @Autowired
    PaperMapper paperMapper;

    @Override
    public Map<String, Integer> queryReportByUserId(Long userId) {

        Map<String, Integer> resultMap = new HashMap<>();
        QueryWrapper<Report> wrapper1 = new QueryWrapper();
        wrapper1.eq("user_id", userId);
        wrapper1.eq("star", 2).orderBy(true, false, "create_time");
        List<Report> reports1 = reportMapper.selectList(wrapper1);

        if (reports1.size() == 0) {
            resultMap.put("easy", 0);
        } else {
            resultMap.put("easy", reports1.get(0).getReportId().intValue());
        }


        QueryWrapper<Report> wrapper2 = new QueryWrapper();
        wrapper2.eq("user_id", userId);
        wrapper2.eq("star", 3).orderBy(true, false, "create_time");
        List<Report> reports2 = reportMapper.selectList(wrapper2);
        if (reports2.size() == 0) {
            resultMap.put("middle", 0);
        } else {
            resultMap.put("middle", reports2.get(0).getReportId().intValue());
        }


        QueryWrapper<Report> wrapper3 = new QueryWrapper();
        wrapper3.eq("user_id", userId);
        wrapper3.eq("star", 5).orderBy(true, false, "create_time");
        List<Report> reports3 = reportMapper.selectList(wrapper3);
        if (reports3.size() == 0) {
            resultMap.put("ultimate", 0);
        } else {
            resultMap.put("ultimate", reports3.get(0).getReportId().intValue());
        }

        return resultMap;

    }

    /**
     * 查询题目并且封装为SoulQuestionVo类型
     *
     * @return
     */
    @Override
    public Map<String, List<SoulQuestionVo>> queryQuestion() {
        Map<String, List<SoulQuestionVo>> resultMap = new HashMap<>();
        QueryWrapper<SoulQuestion> wrapper1 = new QueryWrapper();
        wrapper1.eq("question_star", 2);
        List<SoulQuestion> soulQuestions1 = soulQuestionMapper.selectList(wrapper1);

        //试题数量小于10个不用挑选
        if (soulQuestions1.size() <= 10) {
            List<SoulQuestionVo> soulQuestionVos = queryOption(soulQuestions1);
            resultMap.put("easy", soulQuestionVos);
        } else {
            List<SoulQuestion> newSoulQuestion1 = getSoulQuestion(soulQuestions1, new ArrayList<SoulQuestion>());
            List<SoulQuestionVo> soulQuestionVos1 = queryOption(newSoulQuestion1);
            resultMap.put("easy", soulQuestionVos1);
        }

        QueryWrapper<SoulQuestion> wrapper2 = new QueryWrapper();
        wrapper2.eq("question_star", 3);
        List<SoulQuestion> soulQuestions2 = soulQuestionMapper.selectList(wrapper2);
        if (soulQuestions2.size() <= 10) {
            List<SoulQuestionVo> soulQuestionVos2 = queryOption(soulQuestions2);
            resultMap.put("middle", soulQuestionVos2);
        } else {

            List<SoulQuestion> newSoulQuestion2 = getSoulQuestion(soulQuestions2, new ArrayList<SoulQuestion>());
            List<SoulQuestionVo> soulQuestionVos2 = queryOption(newSoulQuestion2);
            resultMap.put("middle", soulQuestionVos2);
        }

        QueryWrapper<SoulQuestion> wrapper3 = new QueryWrapper();
        wrapper3.eq("question_star", 5);
        List<SoulQuestion> soulQuestions3 = soulQuestionMapper.selectList(wrapper3);
        if (soulQuestions3.size() <= 10) {
            List<SoulQuestionVo> soulQuestionVos = queryOption(soulQuestions3);
            resultMap.put("ultimate", soulQuestionVos);
        } else {

            List<SoulQuestion> newSoulQuestion3 = getSoulQuestion(soulQuestions3, new ArrayList<SoulQuestion>());

            List<SoulQuestionVo> soulQuestionVos3 = queryOption(newSoulQuestion3);
            resultMap.put("ultimate", soulQuestionVos3);
        }
        return resultMap;

    }

    /**
     * 传入一个大于10个长度SoulQuestion的集合,返回一个10个长度的集合,并且去重
     *
     * @param originalList
     * @param resultList
     * @return
     */
    @Override
    public List<SoulQuestion> getSoulQuestion(List<SoulQuestion> originalList, List<SoulQuestion> resultList) {

        Set<Integer> set = new HashSet();
        while (true) {
            int i = RandomUtil.randomInt(0, originalList.size());
            set.add(i);
            if (set.size() >= 10) {
                break;
            }

        }
        for (Integer integer : set) {
            resultList.add(originalList.get(integer));

        }

        return resultList;
    }

    /**
     * 传入一个SoulQuestion的集合,返回每个SoulQuestion的答案.并且封装为SoulQuestionVo的 形式
     *
     * @param originalList
     * @return
     */
    @Override
    public List<SoulQuestionVo> queryOption(List<SoulQuestion> originalList) {
        if (CollUtil.isEmpty(originalList)) {
            return null;
        }
        List<SoulQuestionVo> soulQuestionVos = new ArrayList<>();

        List<Object> questionIds = CollUtil.getFieldValues(originalList, "questionId");
        QueryWrapper<Option> wrapper = new QueryWrapper<>();
        wrapper.in("question_id", questionIds);
        List<Option> options = optionMapper.selectList(wrapper);

        for (SoulQuestion soulQuestion : originalList) {
            SoulQuestionVo soulQuestionVo = new SoulQuestionVo();
            List<OptionsVo> newoptions = new ArrayList<>();
            for (Option option : options) {
                if (soulQuestion.getQuestionId() == option.getQuestionId()) {
                    OptionsVo newOptionsVo = new OptionsVo();
                    newOptionsVo.setId(option.getOptionId().toString());
                    newOptionsVo.setOption(option.getOptionText());
                    newoptions.add(newOptionsVo);
                }
            }
            soulQuestionVo.setId(soulQuestion.getQuestionId().toString());
            soulQuestionVo.setQuestion(soulQuestion.getQuestionText());
            soulQuestionVo.setOptions(newoptions);
            soulQuestionVos.add(soulQuestionVo);
        }


        return soulQuestionVos;
    }

    @Override
    public Paper getPaper(int level) {
        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        wrapper.eq("level", level);
        return paperMapper.selectOne(wrapper);
    }
}
